package com.itheima.cook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;

@WebServlet("/cok1")
public class ServletCook03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("html/text,charset=utf-8");
        resp.getWriter().print("最后的访问时间");
        Cookie cookie = new Cookie("name", System.currentTimeMillis() + "");
        resp.addCookie(cookie);
        cookie.setMaxAge(3600);
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie1 : cookies) {
            if ("name".equals(cookie1.getName())) {
                String value = cookie1.getValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                resp.getWriter().print(sdf.format(Long.parseLong(value)));
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
